<?php
/**
 * @Author: rocky
 * @Copyright: 广州拓冠科技 <http://my8m.com>
 * Date: 2019/8/17
 * Time: 16:21
 */


namespace app\common\service;


use app\common\model\SmsLog;
use think\facade\Cache;

class Sms
{
    /**
     * 发送随机数字验证码
     * @Author: rocky
     * 2019/8/17 16:32
     * @param $phone 手机号码
     * @param int $length 发送随机数长度
     * @param string $type 作用类型
     * @param string $type_desc 作用描述
     * @return bool
     */
    public static function sendRandNumber($phone, $length = 6, $type = 1, $type_desc = '登录',$write_log=true)
    {
        $i = 0;
        $sequence = '';
        while ($i++ < $length) {
            $sequence .= ($i <= 1 ? rand(1, 9) : rand(0, 9));
        }
        return self::send($phone, $sequence, $type, $type_desc,$write_log);
    }

    /**
     * 发送验证码
     * @Author: rocky
     * 2019/8/17 16:32
     * @param $phone 手机号码
     * @param int $code 发送内容
     * @param int $type 作用类型
     * @param string $type_desc 作用描述
     * @return bool
     */
    public static function send($phone, $code, $type, $type_desc,$write_log=true)
    {
        $cacheKey = "{$phone}_$type";
        if (Cache::has("{$cacheKey}_lock")) {
            if($write_log){
                SmsLog::insert([
                    'uid' => \app\common\facade\Token::id(),
                    'phone' => $phone,
                    'content' => $code,
                    'result' => '失败，频繁发送',
                    'result_type'=>0,
                    'type_desc' => $type_desc
                ]);
            }
            return '验证码发送频繁，请稍后再试';
        } else {
            $return = AlismsService::send($phone, config('aliyun.TemplateCode'), config('aliyun.SignName'), ['code' => $code]);
        }
        if ($return['Code'] == 'OK') {
            Cache::set("{$cacheKey}_lock", 1, 60);
            Cache::set($cacheKey, $code, 60 * 10);
            $result = 1;
        } else {
            $result = 0;
        }
        if($write_log) {
            SmsLog::insert([
                'uid' => \app\common\facade\Token::id(),
                'phone' => $phone,
                'content' => $code,
                'result' => $return['Message'],
                'result_type'=>$result,
                'type_desc' => $type_desc
            ]);
        }
        if($result){
            return true;
        }else{
            return '验证码发送失败';
        }
    }

    /**
     * 检测验证码
     * @Author: rocky
     * 2019/8/17 16:39
     * @param $phone 手机号
     * @param $code 验证码
     * @param int $type 作用类型
     * @return bool
     */
    public static function check($phone, $code, $type = 1)
    {
        //万能验证码，开发阶段使用
        if(config('app_debug') && $code == 888888){
            return true;
        }
        $cacheKey = "{$phone}_$type";
        $verify = Cache::get($cacheKey);
        if ($verify == $code && !empty($code)) {
            Cache::rm($cacheKey);
            return true;
        } else {
            return false;
        }
    }
}